//
//  YBTabBar.m
//  WaWaJiClient
//
//  Created by Rookie on 2017/11/15.
//  Copyright © 2017年 zego. All rights reserved.
//

#import "YBTabBar.h"

@interface YBTabBar()

@property (nonatomic,weak) UIView *addBgview;
@property (nonatomic, weak) UIButton *addButton;

@end

@implementation YBTabBar

- (instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        
        UIView *addBgv = [[UIView alloc]init];
        addBgv.backgroundColor = [UIColor clearColor];
        
        [self addSubview:addBgv];
        self.addBgview = addBgv;
        
        UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = addButton.frame;
        //frame.size = addButton.currentBackgroundImage.size;
        frame.size = CGSizeMake(40, 40);
        addButton.frame = frame;
        
        //方式一 图片无文字
        [addButton setBackgroundImage:[UIImage imageNamed:@"tab_center"] forState:UIControlStateNormal];
        [addButton setBackgroundImage:[UIImage imageNamed:@"tab_center"] forState:UIControlStateHighlighted];
        
        //方式二 图片有文字
        /*
        [addButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];
        [addButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateHighlighted];
        [addButton setTitle:@"" forState: UIControlStateNormal];
        [addButton setTitle:@"" forState: UIControlStateHighlighted];
        [addButton setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        [addButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        addButton.titleLabel.font = SYS_Font(11);
        [addButton setImageEdgeInsets:UIEdgeInsetsMake(-(65-addButton.imageView.frame.size.height), 0.0, 0.0, -addButton.titleLabel.frame.size.height)];
        [addButton setTitleEdgeInsets:UIEdgeInsetsMake(-3, -addButton.imageView.frame.size.height, -(65-addButton.titleLabel.frame.size.height), 0.0)];
        */
        [addButton addTarget:self action:@selector(publishClick) forControlEvents:UIControlEventTouchUpInside];
        /** 预留*/
        [self addSubview:addButton];
        self.addButton = addButton;
        
    }
    return self;
}
/**
 * 中间按钮点击事件
 */
- (void)publishClick{
    if ([_tabbarDelegate respondsToSelector:@selector(centerBtnDidClicked)]) {
        [_tabbarDelegate centerBtnDidClicked];
    }
}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height - ShowDiff;
   
    /**
     *  单独设置中间的按钮
     */
    self.addButton.center = CGPointMake(width * 0.5, height * 0.5);
    self.addBgview.frame = CGRectMake(width*2/5, 0, width/5, height);
    CGFloat buttonY = 0;
    CGFloat buttonW = width / 5;
    CGFloat buttonH = height;
    NSInteger index = 0;
    for (UIControl *button in self.subviews) {
        if (![button isKindOfClass:[UIControl class]] || button == self.addButton) continue;
        /** 中间空出 */
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        /** 中间不空 */
//        CGFloat buttonX = buttonW * index;
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        index++;
    }
}


@end
